package com.vitalfit.controller;

import com.vitalfit.annotation.LoginUser;
import com.vitalfit.enums.ResultEnum;
import com.vitalfit.model.dto.FoodOrderDTO;
import com.vitalfit.model.entity.food.FoodOrder;
import com.vitalfit.model.vo.PageFoodOrderVO;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.model.vo.UserAuthentication;
import com.vitalfit.service.serviceImpl.FoodOrderServiceImpl;
import com.vitalfit.utils.ResultVOUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/food-orders")
@Validated
public class FoodOrderController {

    private final FoodOrderServiceImpl foodOrderService;


    /**
     * 创建订单
     * @param foodOrderDTO 订单的数据传输对象
     * @return 创建的订单实体
     */
    @PreAuthorize("hasRole('ROLE_CUSTOMER')")
    @PostMapping
    public ResultVO<FoodOrder> createFoodOrder(@LoginUser UserAuthentication userAuthentication,
                                               @Valid @RequestBody FoodOrderDTO foodOrderDTO) {
        FoodOrder foodOrder = foodOrderService.createFoodOrder(userAuthentication,foodOrderDTO);
        return ResultVOUtil.buildResult(ResultEnum.POST_SUCCESS,foodOrder);
    }

    /**
     * 更新订单状态
     * @param foodOrderDTO 订单的数据传输对象
     * @return 更新后的订单实体
     */
    @PreAuthorize("hasRole('ROLE_MANAGER')")
    @PutMapping("/status")
    public ResultVO<FoodOrder> updateOrderStatus(@Valid @RequestBody FoodOrderDTO foodOrderDTO) {
        FoodOrder foodOrder = foodOrderService.updateFoodOrderStatus(foodOrderDTO);
        return ResultVOUtil.buildResult(ResultEnum.PUT_SUCCESS,foodOrder);
    }

    /**
     * 为订单评分
     * @param orderId 订单ID
     * @param rating 评分
     * @return 更新后的订单实体
     */
    @PreAuthorize("hasRole('ROLE_CUSTOMER')")
    @PostMapping("/rate/{orderId}")
    public ResultVO<FoodOrder> rateOrder(@PathVariable Integer orderId, @RequestParam Integer rating) {
        FoodOrder foodOrder = foodOrderService.rateFoodOrder(orderId, rating);
        return ResultVOUtil.buildResult(ResultEnum.PUT_SUCCESS,foodOrder);
    }

    /**
     * 获取指定ID的订单
     * @param orderId 订单ID
     * @return 请求的订单实体
     */
    @PreAuthorize("hasRole('ROLE_CUSTOMER') or hasRole('ROLE_SHOPMANAGER')")
    @GetMapping("/{orderId}")
    public ResultVO<FoodOrder> getOrder(@PathVariable Integer orderId) {
        FoodOrder foodOrder = foodOrderService.getFoodOrder(orderId);
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,foodOrder);
    }

    @PreAuthorize("hasRole('ROLE_CUSTOMER') or hasRole('ROLE_SHOPMANAGER')")
    @GetMapping("/user")
    public ResultVO<PageFoodOrderVO> getOrderByUserId(@LoginUser UserAuthentication userAuthentication,
                                                      @RequestParam(defaultValue = "0") int page,
                                                      @RequestParam(defaultValue = "10") int size) {

        PageFoodOrderVO pageFoodOrderVO = foodOrderService.getFoodOrderByUserId(userAuthentication.getUserId(),page,size);
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,pageFoodOrderVO);
    }

    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @GetMapping("/all")
    public ResultVO<PageFoodOrderVO> getAllOrder(@RequestParam(defaultValue = "0") int page,
                                                      @RequestParam(defaultValue = "10") int size) {

        PageFoodOrderVO pageFoodOrderVO = foodOrderService.getAllFoodOrder(page,size);
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,pageFoodOrderVO);
    }

}